package com.tea.room.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.tea.room.model.Return;
import com.tea.room.service.door.DoorService;

/**
 * 门锁接口
 * @ClassName:DoorController   
 * @Description:TODO()   
 * @date:2019年6月5日 上午8:25:27 
 * @author ShiP
 */
@RestController("doorController")
public class DoorController {
    
    @Autowired
    private DoorService doorService;
    
    /**
     * 查询门锁电池和信号状态
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/queryLockBattery")
    public Return queryLockBattery(HttpServletRequest request, HttpServletResponse response){
        Return rn = new Return();
        List<String> list = new ArrayList<String>();
        list.add(request.getParameter("lockId"));
        String battery = doorService.queryLockBattery(list);
        rn.setMessage(battery);
        return rn;
    }
    
    /**
     * 通过本接口执行远程开锁操作
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/remoteUnlock")
    public Return remoteUnlock(HttpServletRequest request, HttpServletResponse response){
        Return rn = new Return();
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("lockId", request.getParameter("lockId"));
        param.put("hardwareId", request.getParameter("hardwareId"));
        String unlock = doorService.remoteUnlock(param);
        rn.setMessage(unlock);
        return rn;
    }
    
    /**
     * 通过本接口查询门锁的操作结果（远程开锁等）
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/queryRemoteUnlock")
    public Return queryRemoteUnlock(HttpServletRequest request, HttpServletResponse response){
        Return rn = new Return();
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("operationId", request.getParameter("operationId"));
        param.put("hardwareId", request.getParameter("hardwareId"));
        param.put("requestType", request.getParameter("requestType"));
        String status = doorService.queryOperationStatus(param);
        rn.setMessage(status);
        return rn;
    }
    
    /**
     * 下发在线密码
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/onlinePasswordKey")
    public Return onlinePasswordKey(HttpServletRequest request, HttpServletResponse response){
        Return rn = new Return();
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("lockId", request.getParameter("lockId"));
        param.put("validStart", request.getParameter("validStart"));
        param.put("validUntil", request.getParameter("validUntil"));
        param.put("holder", request.getParameter("holder"));
        param.put("phoneNumber", request.getParameter("phoneNumber"));
        param.put("password", request.getParameter("password"));
        param.put("subType", request.getParameter("subType"));
        param.put("canOpenAntilock", request.getParameter("canOpenAntilock"));
        param.put("phoneCountry", "+86");
        String password = doorService.onlinePassword(param);
        rn.setMessage(password);
        return rn;
    }
    
    /**
     * 查询下发在线密码结果
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/queryOnlinePasswordKey")
    public Return queryOnlinePasswordKey(HttpServletRequest request, HttpServletResponse response){
        Return rn = new Return();
        String operationId = request.getParameter("operationId");
        String hardwareId = request.getParameter("hardwareId");
        String status = doorService.queryPasswordStatus(operationId, hardwareId);
        rn.setMessage(status);
        return rn;
    }
    
    /**
     * 获取楼栋列表 
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/getBuilding")
    public Return getBuilding(HttpServletRequest request, HttpServletResponse response){
        Return rn = new Return();
        Map<String, Object> param = new HashMap<String, Object>();
        String status = doorService.getBuilding(param);
        rn.setMessage(status);
        return rn;
    }
    
    /**
     * getFloor
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/getFloor")
    public Return getFloor(HttpServletRequest request, HttpServletResponse response){
        Return rn = new Return();
        Map<String, Object> param = new HashMap<String, Object>();
        String status = doorService.getFloor(param);
        rn.setMessage(status);
        return rn;
    }
    
    /**
     * getRoomList
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/getRoomList")
    public Return getRoomList(HttpServletRequest request, HttpServletResponse response){
        Return rn = new Return();
        Map<String, Object> param = new HashMap<String, Object>();
        String status = doorService.getRoomList(param);
        rn.setMessage(status);
        return rn;
    }
    
    /**
     * getRoom
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/getRoom")
    public Return getRoom(HttpServletRequest request, HttpServletResponse response){
        Return rn = new Return();
        String building = request.getParameter("building");
        String floor = request.getParameter("floor");
        String room = request.getParameter("room");
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("building", building);// 楼栋名字
        param.put("floor", floor);// 楼层名字
        param.put("room", room);// 房间名字
        String status = doorService.getRoom(param);
        rn.setMessage(status);
        return rn;
    }
    
    /**
     * getDoorlock
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/getDoorlock")
    public Return getDoorlock(HttpServletRequest request, HttpServletResponse response){
        Return rn = new Return();
        String placeId = request.getParameter("placeId");
        String status = doorService.getDoorlock(placeId);
        rn.setMessage(status);
        return rn;
    }
}
